import Vue from "vue";
import Vuex from "vuex";
import http from "../utils/fetch";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    userInfo: {},
    menuInfo: null,
    btnPermission: [],
    // -----------------------------------------------------------------------------------------------
    //仓库,
    wms: "ENTERPRISE",
    //当前页面菜单id
    wmsId: null,
    //所有页面按键集合
    wmsBtnPermissions: [],
    optionsObject: {},
    //主页跳转传的值
    routeData: {},
    //主页新建id值
    routeId: "",
    //主页新建开关
    newFlag: false,
    //临时数据存放地方
    temporary: "",
    //临时存储点击的ASN/收货单对象
    tempAsnObj: {},
    //详情中新建
    detailsNew: false,
    skip: {},
    closeFlag: true,
    iframeSrc: [],
    //补货登录的对象信息
    bhObjUs: {},
    frameShow: true,
    frameUrl: "",
    isRouterAlive: true,
    dropDisabled: false, //仓库选择禁用状态
  },
  mutations: {
    SET_USERINFO(state, data) {
      sessionStorage.setItem("userInfo", JSON.stringify(data));
      state.userInfo = data;
    },
    SET_MENUINFO(state, data) {
      state.menuInfo = data;
    },
    SET_BTNINFO(state, data) {
      state.btnPermission = data;
    },
    // -----------------------------------------------------------------------------------------
    //设置仓库
    setWms(state, data) {
      state.wms = data;
    },
    setWmsId(state, data) {
      state.wmsId = data;
    },
    //设置select框下拉的值
    setOptionsObject(state, data) {
      state.optionsObject = data;
    },
    //设置routeData的值
    setRouteData(state, data) {
      state.routeData = data;
    },
    //设置routeId值
    setRouteId(state, data) {
      state.routeId = data;
    },
    //设置newFlag值（true，false）
    setNewFlag(state, data) {
      state.newFlag = data;
    },
    //设置临时数据
    setTemporary(state, data) {
      state.temporary = data;
    },
    //设置临时asn/收货单对象
    setTempAsnObj(state, obj) {
      state.tempAsnObj = obj;
    },
    //设置详情新建状态
    setDetailsNew(state, data) {
      state.detailsNew = data;
    },
    //设置
    setCloseFlag(state, data) {
      state.closeFlag = data;
    },
    setSkip(state, data) {
      state.skip = data;
    },
    setIframeSrc(state, data) {
      if (data.type === "array") {
        state.iframeSrc = data.data;
      } else {
        let flag = state.iframeSrc.some((item) => {
          return item.title === data.title;
        });
        if (!flag) {
          state.iframeSrc.push(data);
        }
      }
    },
    //设置bhObjUs的值
    setBhObjUs(state, data) {
      state.bhObjUs = data;
    },
    setFrameShow(state, data) {
      state.frameShow = data;
    },
    setFrameUrl(state, data) {
      state.frameUrl = data;
    },
    setIsRouterAlive(state, bol) {
      state.isRouterAlive = bol;
    },
    setBtnPermissions(state, data) {
      state.wmsBtnPermissions = data;
    },
    setDropDisabled(state, bol) {
      state.dropDisabled = bol;
    },
  },
  actions: {
    getMenuInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        http
          .get("/menuPermission/getUserPermissionMenuInfo")
          .then((response) => {
            const { data } = response;
            commit("SET_MENUINFO", data);
            Compartmentalize(data);
            commit("SET_BTNINFO", btnPermission);
            sessionStorage.setItem("btnPermission",JSON.stringify(btnPermission))
            resolve(data);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
  getters: {
    userInfo(state) {
      return state.userInfo;
    },
    menuInfo(state) {
      return state.menuInfo;
    },
    btnPermission(state) {
      return state.btnPermission;
    },
    // ---------------------------------------------------------------------------
    routeData(state) {
      return state.routeData;
    },
    btnCodeList(state) {
      return state.wmsBtnPermissions.reduce(
        (pre, cur) => pre.concat(cur.node.map((n) => n.name)),
        []
      );
    },
    dropDisabled(state) {
      return state.dropDisabled;
    },
    wms(state) {
      return state.wms;
    },
  },
  modules: {},
});

let btnPermission = [];
function Compartmentalize(DATA) {
  DATA.forEach((item) => {
    if (Array.isArray(item.buttons) && item.buttons.length > 0) {
      item.buttons.forEach(btn => {
        btnPermission.push(btn.code)
      })
    }
    if (Array.isArray(item.nodes) && item.nodes.length > 0) {
      Compartmentalize(item.nodes, btnPermission);
    }
  });
}
